<template>
  <div class="navBox">
    <div class="navClose" v-if="!open">
      <scroll-view
        class="scroll"
        scroll-x
        :scroll-into-view="scrollToNode"
        :scroll-with-animation="true"
      >
        <div class="navList">
          <div
            :class="['navItem', choosedCat.id == item.id ? 'choosed' : '']"
            v-for="(item, index) in dataList"
            :key="item.id"
            @click.stop="changeNav(item, index)"
            :id="'menu' + index"
          >
            <div class="navPic">
              <image :src="item.pic_url" alt="" />
            </div>
            <span>{{ item.name }}</span>
          </div>
        </div>
      </scroll-view>
      <div class="open" @click.stop="open = !open">
        <span>{{ $t("diy.open1") }}</span>
        <span>{{ $t("diy.open2") }}</span>
        <span class="iconfont icon-arrow-down"></span>
      </div>
    </div>
    <div class="navOpenBox" v-else @click.stop="open = false">
      <div class="navOpen">
        <div class="navList">
          <div class="itemBox" v-for="item in dataList" :key="item.id">
            <div
              :class="['navItem', choosedCat.id == item.id ? 'choosed' : '']"
              @click.stop="changeNav(item)"
            >
              <div class="navPic">
                <img :src="item.pic_url" alt="" />
              </div>
              <span>{{ item.name }}</span>
            </div>
          </div>
        </div>
        <div class="close" @click.stop="open = !open">
          <span>{{ $t("shareholder.close") }}</span>
          <span class="iconfont icon-arrow-up"></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  props: {
    dataList: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  watch: {
    dataList: {
      handler() {
        // #ifdef MP-ALIPAY
        if (!this.first) {
          this.init();
          this.first = true;
        }
        // #endif
        // #ifndef MP-ALIPAY
        this.init();
        // #endif
      },
      immediate: true,
      deep: true
    },
    getGoCatId: {
      handler() {
        this.$nextTick(() => {
          this.init();
        });
      },
      immediate: true
    }
  },
  computed: {
    ...mapGetters(["getGoCatId"])
  },
  data() {
    return {
      open: false,
      choosedCat: {},
      scrollToNode: "",
      first: false
    };
  },
  mounted() {
    // if (this.dataList.length > 0) {
    //   this.choosedCat = this.dataList[0];
    //   this.$emit("changeNav", this.choosedCat);
    // }
  },
  methods: {
    init() {
      if (this.dataList.length) {
        let e = this.getGoCatId,
          { dataList } = this,
          index = 0,
          item = dataList.find((el) => el.id === e);

        if (item) {
          index = dataList.findIndex((el) => el.id === e);
        } else {
          try {
            if (dataList.findIndex((el) => el.id === e) !== index) {
              let newList = this.$utils.getFlatArray(dataList);
              let parent_id = newList.find((el) => el.id === e).parent_id,
                parent = newList.find((el) => el.id === parent_id);
              index = dataList.findIndex((el) => el.id === parent.id);
            }
          } catch (error) {}
        }

        item = dataList[index];
        this.$nextTick(() => {
          this.changeNav(item, index);
        });
      }
    },
    // 切换导航
    changeNav(e, index) {
      if (this.choosedCat.id != e.id) {
        let id = this.choosedCat.id;
        let i = this.dataList.findIndex((item) => {
          return item.id == id;
        });
        if (index >= i) {
          if (index + 1 <= this.dataList.length)
            this.scrollToNode = "menu" + (index - 1);
        } else {
          if (index - 2 >= -1) this.scrollToNode = "menu" + (index - 1);
        }
        this.choosedCat = e;
        this.$emit("changeNav", e);
      }
    },
    refresh() {
      if (this.dataList.length) {
        this.choosedCat = this.dataList[0];
        this.scrollToNode = "menu0";
        this.$emit("changeNav", this.choosedCat);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.navBox {
  width: 100%;
}
.navOpenBox {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  box-sizing: border-box;
}
.navClose {
  width: 100%;
  height: 168rpx;
  background-color: #fff;
  display: flex;
  .scroll {
    flex: auto;
    width: 0;
    height: 100%;
    .navList {
      padding-top: 6rpx;
      padding-left: 32rpx;
      display: flex;
      flex-wrap: nowrap;
      .navItem {
        margin-right: 20rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        .navPic {
          width: 92rpx;
          height: 92rpx;
          border-radius: 50%;
          margin-bottom: 12rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          image {
            width: 80rpx;
            height: 80rpx;
            border-radius: 50%;
          }
        }
        span {
          font-size: 22rpx;
          color: #262626;
          white-space: nowrap;
        }
      }
      .choosed {
        .navPic {
          border: 1px solid #f0250e;
        }
        span {
          padding: 4rpx 16rpx;
          background-color: #f0250e;
          border-radius: 99px;
          color: #fff;
          font-weight: 600;
        }
      }
    }
  }
  .open {
    width: 72rpx;
    min-width: 72rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 24rpx;
    color: #262626;
    font-weight: 600;
    font-family: PingFang SC;
    box-shadow: -20rpx 0px 12rpx -12rpx rgba(0, 0, 0, 0.08);
    cursor: pointer;
    .iconfont {
      font-size: 28rpx;
    }
  }
}
.navOpen {
  width: 100%;
  background-color: #fff;
  border-radius: 0 0 24rpx 24rpx;
  .navList {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .itemBox {
      width: 20%;
      display: flex;
      justify-content: center;
      .navItem {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin: 10rpx;
        box-sizing: border-box;
        .navPic {
          width: 112rpx;
          height: 112rpx;
          border-radius: 50%;
          margin-bottom: 12rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          box-sizing: border-box;
          img {
            width: 92rpx;
            height: 92rpx;
            border-radius: 50%;
          }
        }
        span {
          font-size: 22rpx;
          color: #262626;
          white-space: nowrap;
        }
      }
      .choosed {
        .navPic {
          border: 1px solid #f0250e;
        }
        span {
          padding: 4rpx 12rpx;
          background-color: #f0250e;
          border-radius: 99px;
          color: #fff;
          font-weight: 600;
        }
      }
    }
  }
  .close {
    cursor: pointer;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 60rpx;
    border-top: 1px solid #f6f6f6;
    font-size: 24rpx;
    color: #8c8c8c;
    .iconfont {
      font-size: 28rpx;
      color: #262626;
      margin-left: 12rpx;
    }
  }
}
</style>
